Technical Q&A JAVA21
Moving Files in Java


Q: How do I move a file from one directory to another? Do I have to use the Mac OS Toolbox?

A: As long as the files are on the same volume, you just need to use the java.io.File.renameTo() method. Using this call for files and directories works equally well, but due to some platform-specific runtime implementation differences, this is one of those things that falls into the write once, test multiple places, and tweak until done catagories. This call will fail if you try to move a file from one volume to another.

Moving to another volume is more difficult, since this actually involves copying the file, and there is no standard Java call to copy a file. You would need to create your own, and on the Mac this becomes more complicated because you must worry about resource forks and file types. For this, you would need to use JDirect, or possibly JConfig or Greg Guerin's MacBinary Toolkit.

[Feb 02 2000]


Developer Documentation | Technical Notes | Development Kits | Sample Code